home *** CD-ROM | disk | FTP | other *** search
/ CD Actual Thematic 7: Programming / CDAT7.iso / demos / VisualAge for Java 2.0 Entry / setup / data1.cab / ide-e / IDE / cache / J2K95Q (.txt) < prev    next >
Encoding:
Java Class File  |  1998-09-16  |  6.9 KB  |  180 lines

  1. package com.sun.java.swing.plaf.basic;
  2.  
  3. import com.sun.java.swing.AbstractButton;
  4. import com.sun.java.swing.Icon;
  5. import com.sun.java.swing.JComponent;
  6. import com.sun.java.swing.JMenuItem;
  7. import com.sun.java.swing.LookAndFeel;
  8. import com.sun.java.swing.MenuElement;
  9. import com.sun.java.swing.MenuSelectionManager;
  10. import com.sun.java.swing.UIManager;
  11. import com.sun.java.swing.plaf.ComponentUI;
  12. import com.sun.java.swing.plaf.MenuItemUI;
  13. import com.sun.java.swing.plaf.UIResource;
  14. import java.awt.AWTEvent;
  15. import java.awt.Color;
  16. import java.awt.Component;
  17. import java.awt.Dimension;
  18. import java.awt.Graphics;
  19. import java.awt.Insets;
  20. import java.awt.Point;
  21. import java.awt.event.InputEvent;
  22. import java.awt.event.KeyEvent;
  23. import java.awt.event.MouseEvent;
  24. import java.awt.event.MouseListener;
  25. import java.awt.event.MouseMotionListener;
  26. import java.io.Serializable;
  27.  
  28. public class BasicMenuItemUI extends MenuItemUI implements Serializable {
  29.    protected static Color pressedBackground;
  30.    protected static Color pressedForeground;
  31.    protected static final int defaultTextIconGap = 4;
  32.    protected MouseListener mouseListener;
  33.    protected MouseMotionListener dragListener;
  34.    protected Icon menuArrow = null;
  35.    protected Icon checkIcon = null;
  36.    protected boolean oldBorderPainted;
  37.  
  38.    protected void addListeners(JComponent c) {
  39.       ((Component)c).addMouseListener(this.mouseListener);
  40.       ((Component)c).addMouseMotionListener(this.dragListener);
  41.    }
  42.  
  43.    protected MouseListener createMouseListener(JComponent c) {
  44.       return new BasicMenuMouseListener();
  45.    }
  46.  
  47.    protected MouseMotionListener createMouseMotionListener(JComponent c) {
  48.       return new BasicMenuMouseMotionListener();
  49.    }
  50.  
  51.    public static ComponentUI createUI(JComponent c) {
  52.       return new BasicMenuItemUI();
  53.    }
  54.  
  55.    Icon getCheckIcon() {
  56.       return this.checkIcon;
  57.    }
  58.  
  59.    public Insets getDefaultMargin(AbstractButton c) {
  60.       return new Insets(2, 2, 2, 2);
  61.    }
  62.  
  63.    public Dimension getMaximumSize(JComponent c) {
  64.       return this.getPreferredSize(c);
  65.    }
  66.  
  67.    Icon getMenuArrow() {
  68.       return this.menuArrow;
  69.    }
  70.  
  71.    public Dimension getMinimumSize(JComponent c) {
  72.       return this.getPreferredSize(c);
  73.    }
  74.  
  75.    public Dimension getPreferredSize(JComponent c) {
  76.       Icon checkIcon = this.getCheckIcon();
  77.       Icon arrowIcon = this.getMenuArrow();
  78.       return BasicGraphicsUtils.getPreferredMenuItemSize(c, checkIcon, arrowIcon, 4);
  79.    }
  80.  
  81.    protected void initListeners(JComponent c) {
  82.       this.mouseListener = this.createMouseListener(c);
  83.       this.dragListener = this.createMouseMotionListener(c);
  84.    }
  85.  
  86.    public void installUI(JComponent c) {
  87.       JMenuItem menuItem = (JMenuItem)c;
  88.       this.initListeners(c);
  89.       this.addListeners(c);
  90.       c.setOpaque(true);
  91.       LookAndFeel.installBorder(c, "MenuItem.border");
  92.       this.oldBorderPainted = ((AbstractButton)menuItem).isBorderPainted();
  93.       ((AbstractButton)menuItem).setBorderPainted((Boolean)UIManager.get("MenuItem.borderPainted"));
  94.       LookAndFeel.installColorsAndFont(c, "MenuItem.background", "MenuItem.foreground", "MenuItem.font");
  95.       if (pressedBackground == null || pressedBackground instanceof UIResource) {
  96.          pressedBackground = UIManager.getColor("MenuItem.pressedBackground");
  97.       }
  98.  
  99.       if (pressedForeground == null || pressedForeground instanceof UIResource) {
  100.          pressedForeground = UIManager.getColor("MenuItem.pressedForeground");
  101.       }
  102.  
  103.       if (this.menuArrow == null || this.menuArrow instanceof UIResource) {
  104.          this.menuArrow = UIManager.getIcon("MenuItem.arrowIcon");
  105.       }
  106.  
  107.       if (this.checkIcon == null || this.checkIcon instanceof UIResource) {
  108.          this.checkIcon = UIManager.getIcon("MenuItem.checkIcon");
  109.       }
  110.  
  111.    }
  112.  
  113.    private int lower(int ascii) {
  114.       return ascii >= 65 && ascii <= 90 ? ascii + 97 - 65 : ascii;
  115.    }
  116.  
  117.    public void paint(Graphics g, JComponent c) {
  118.       BasicGraphicsUtils.paintMenuItem(g, c, this.getCheckIcon(), this.getMenuArrow(), pressedBackground, pressedForeground, 4);
  119.    }
  120.  
  121.    public void processKeyEvent(JMenuItem item, KeyEvent e, MenuElement[] path, MenuSelectionManager manager) {
  122.       int key = ((AbstractButton)item).getMnemonic();
  123.       if (key != 0) {
  124.          if (((AWTEvent)e).getID() == 401 && this.lower(key) == this.lower(e.getKeyChar())) {
  125.             manager.clearSelectedPath();
  126.             ((AbstractButton)item).doClick(0);
  127.             ((InputEvent)e).consume();
  128.          }
  129.  
  130.       }
  131.    }
  132.  
  133.    public void processMouseEvent(JMenuItem item, MouseEvent e, MenuElement[] path, MenuSelectionManager manager) {
  134.       Point p = e.getPoint();
  135.       if (p.x >= 0 && p.x < ((JComponent)item).getWidth() && p.y >= 0 && p.y < ((JComponent)item).getHeight()) {
  136.          if (((AWTEvent)e).getID() == 502) {
  137.             manager.clearSelectedPath();
  138.             ((AbstractButton)item).doClick(0);
  139.          } else {
  140.             manager.setSelectedPath(path);
  141.          }
  142.       } else if (((AWTEvent)e).getID() == 502) {
  143.          manager.clearSelectedPath();
  144.       } else if (((AbstractButton)item).getModel().isArmed()) {
  145.          MenuElement[] newPath = new MenuElement[path.length - 1];
  146.          int i = 0;
  147.  
  148.          for(int c = path.length - 1; i < c; ++i) {
  149.             newPath[i] = path[i];
  150.          }
  151.  
  152.          manager.setSelectedPath(newPath);
  153.       }
  154.  
  155.    }
  156.  
  157.    protected void removeListeners(JComponent c) {
  158.       ((Component)c).removeMouseListener(this.mouseListener);
  159.       ((Component)c).removeMouseMotionListener(this.dragListener);
  160.    }
  161.  
  162.    public void uninstallUI(JComponent c) {
  163.       this.removeListeners(c);
  164.       LookAndFeel.uninstallBorder(c);
  165.       ((JMenuItem)c).setBorderPainted(this.oldBorderPainted);
  166.       if (this.menuArrow instanceof UIResource) {
  167.          this.menuArrow = null;
  168.       }
  169.  
  170.       if (this.checkIcon instanceof UIResource) {
  171.          this.checkIcon = null;
  172.       }
  173.  
  174.    }
  175.  
  176.    public void update(Graphics g, JComponent c) {
  177.       this.paint(g, c);
  178.    }
  179. }
  180.